<template>
  <!-- 最外层的大盒子 -->
  <div class="big-box">
    <div class="container">
      <div class="welcome">
        <div class="pinkbox">
          <div class="signin">
            <h1>register</h1>
            <el-form ref="registerRef" :model="registerForm" :rules="registerRules" class="more-padding">
              <div v-if="!tenantEnabled" style="height: 40px"></div>
              <el-form-item v-if="tenantEnabled" prop="tenantId">
                <el-select v-model="registerForm.tenantId" filterable placeholder="请选择/输入公司名称"
                           style="width: 14.2rem;">
                  <el-option v-for="item in tenantList" :key="item.tenantId" :label="item.companyName"
                             :value="item.tenantId"></el-option>
                  <template #prefix>
                    <svg-icon icon-class="company" class="el-input__icon input-icon"/>
                  </template>
                </el-select>
              </el-form-item>
              <el-form-item prop="username">
                <el-input v-model="registerForm.username" auto-complete="off" placeholder="账号" size="large"
                          type="text">
                  <template #prefix>
                    <svg-icon class="el-input__icon input-icon" icon-class="user"/>
                  </template>
                </el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input v-model="registerForm.password" auto-complete="off" placeholder="密码" size="large"
                          type="password" @keyup.enter="handleRegister">
                  <template #prefix>
                    <svg-icon class="el-input__icon input-icon" icon-class="password"/>
                  </template>
                </el-input>
              </el-form-item>
              <el-form-item prop="confirmPassword">
                <el-input
                  v-model="registerForm.confirmPassword"
                  auto-complete="off"
                  placeholder="确认密码"
                  size="large"
                  type="password"
                  @keyup.enter="handleRegister"
                >
                  <template #prefix>
                    <svg-icon class="el-input__icon input-icon" icon-class="password"/>
                  </template>
                </el-input>
              </el-form-item>
              <el-form-item v-if="captchaEnabled" prop="code" class="ml-12">
                <el-input v-model="registerForm.code" auto-complete="off" placeholder="验证码" size="large"
                          style="width: 50%" @keyup.enter="handleRegister">
                  <template #prefix>
                    <svg-icon class="el-input__icon input-icon" icon-class="validCode"/>
                  </template>
                </el-input>
                <div class="login-code">
                  <img :src="codeUrl" class="register-code-img" @click="getCode"/>
                </div>
              </el-form-item>
              <el-form-item>
                <el-button :loading="loading" size="large" style="width:100%;" type="primary"
                           @click.prevent="handleRegister" class="button submit">
                  <svg-icon icon-class="register" class="mr-5"/>
                  <span v-if="!loading">注 册</span>
                  <span v-else>注 册 中...</span>
                </el-button>
              </el-form-item>
            </el-form>

          </div>
        </div>
        <div class="rightbox">
          <h2 class="title"><span>饱了么</span>&<br>后台管理系统</h2>
          <p class="desc"> 成就万千 <span>饿鬼</span></p>
          <img class="flower " src="@/assets/logo/logo.png" alt="饱了么">
          <br><br>
          <p class="account">已有账号?</p>
          <el-button class="button " style="padding: 8px 12px">
            <svg-icon icon-class="login" class="mr-5"/>
            <router-link :to="'/login'">使用已有账户登录</router-link>
          </el-button>


        </div>
      </div>
    </div>
  </div>


</template>

<script setup lang="ts">
import {getCodeImg, register, getTenantList} from '@/api/login';
import {RegisterForm, TenantVO} from '@/api/types';
import {to} from 'await-to-js';

const router = useRouter();

const registerForm = ref<RegisterForm>({
  tenantId: '',
  username: '',
  password: '',
  confirmPassword: '',
  code: '',
  uuid: '',
  userType: 'sys_user'
});

// 租户开关
const tenantEnabled = ref(true);

const equalToPassword = (rule: any, value: string, callback: any) => {
  if (registerForm.value.password !== value) {
    callback(new Error('两次输入的密码不一致'));
  } else {
    callback();
  }
};

const registerRules: ElFormRules = {
  tenantId: [{required: true, trigger: 'blur', message: '请输入您的租户编号'}],
  username: [
    {required: true, trigger: 'blur', message: '请输入您的账号'},
    {min: 2, max: 20, message: '用户账号长度必须介于 2 和 20 之间', trigger: 'blur'}
  ],
  password: [
    {required: true, trigger: 'blur', message: '请输入您的密码'},
    {min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur'},
    {pattern: /^[^<>"'|\\]+$/, message: '不能包含非法字符：< > " \' \\\ |', trigger: 'blur'}
  ],
  confirmPassword: [
    {required: true, trigger: 'blur', message: '请再次输入您的密码'},
    {required: true, validator: equalToPassword, trigger: 'blur'}
  ],
  code: [{required: true, trigger: 'change', message: '请输入验证码'}]
};
const codeUrl = ref('');
const loading = ref(false);
const captchaEnabled = ref(true);
const registerRef = ref<ElFormInstance>();
// 租户列表
const tenantList = ref<TenantVO[]>([]);

const handleRegister = () => {
  registerRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      loading.value = true;
      const [err] = await to(register(registerForm.value));
      if (!err) {
        const username = registerForm.value.username;
         ElNotification({
          title: '注册成功',
          message: '欢迎使用饱了么后台管理系统',
          type: 'success'
        })
        // await ElMessageBox.alert("<font color='red'>恭喜你，您的账号 " + username + ' 注册成功！</font>', '系统提示', {
        //   dangerouslyUseHTMLString: true,
        //   type: 'success'
        // });
        await router.push('/login');
      } else {
        loading.value = false;
        if (captchaEnabled.value) {
          await getCode();
        }
      }
    }
  });
};

const getCode = async () => {
  const res = await getCodeImg();
  const {data} = res;
  captchaEnabled.value = data.captchaEnabled === undefined ? true : data.captchaEnabled;
  if (captchaEnabled.value) {
    codeUrl.value = 'data:image/gif;base64,' + data.img;
    registerForm.value.uuid = data.uuid;
  }
};

const initTenantList = async () => {
  const {data} = await getTenantList();
  tenantEnabled.value = data.tenantEnabled === undefined ? true : data.tenantEnabled;
  if (tenantEnabled.value) {
    tenantList.value = data.voList;
    if (tenantList.value != null && tenantList.value.length !== 0) {
      registerForm.value.tenantId = tenantList.value[0].tenantId;
    }
  }
};

onMounted(() => {
  getCode();
  initTenantList();
});
</script>

<style lang="scss" scoped>
.big-box {
  //display: flex;
  /* 溢出隐藏 */
  height: 100vh;
  overflow-x: hidden;
  background: url("@/assets/images/bg.png") no-repeat center center;
  background-size: cover;
}

@import url("https://fonts.googleapis.com/css?family=Open+Sans:300,400|Lora");

/* div box styling */
.container {
  margin: auto;
  width: 650px;
  height: 550px;
  position: relative;
}

.welcome {
  background: #f6f6f6;
  width: 650px;
  height: 415px;
  position: absolute;
  top: 25%;
  border-radius: 5px;
  box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.1);
}

.pinkbox {
  position: absolute;
  top: -10%;
  left: 5%;
  background: #EAC7CC;
  width: 320px;
  height: 500px;
  border-radius: 5px;
  box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
  transition: all 0.5s ease-in-out;
  z-index: 2;
}

.nodisplay {
  display: none;
  transition: all 0.5s ease;
}

.leftbox, .rightbox {
  position: absolute;
  width: 50%;
  transition: 1s all ease;
}

.leftbox {
  left: -2%;
}

.rightbox {
  right: -2%;
}

/* font & button styling */
h1 {
  font-family: "Open Sans", sans-serif;
  text-align: center;
  margin-top: 65px;
  text-transform: uppercase;
  color: #f6f6f6;
  font-size: 2em;
  letter-spacing: 8px;
}

.title {
  font-family: "Lora", serif;
  color: #8E9AAF;
  font-size: 1.8em;
  line-height: 1.1em;
  letter-spacing: 3px;
  text-align: center;
  font-weight: 300;
  margin-top: 20%;
  margin-bottom: 20px;
}

.desc {
  margin-top: -8px;
}

.account {
  margin-top: 35%;
  font-size: 10px;
}

p {
  font-family: "Open Sans", sans-serif;
  font-size: 0.7em;
  letter-spacing: 2px;
  color: #8E9AAF;
  text-align: center;
}

span {
  color: #EAC7CC;
}

.flower {
  position: absolute;
  width: 120px;
  height: 120px;
  top: 45%;
  left: 29%;
  opacity: 0.7;
}

.smaller {
  width: 90px;
  height: 100px;
  top: 48%;
  left: 38%;
  opacity: 0.9;
}

button {
  padding: 12px;
  font-family: "Open Sans", sans-serif;
  text-transform: uppercase;
  letter-spacing: 3px;
  font-size: 11px;
  border-radius: 10px;
  margin: auto;
  outline: none;
  display: block;
}

button:hover {
  background: #EAC7CC;
  color: #f6f6f6;
  transition: background-color 1s ease-out;
}

.button {
  margin-top: 3%;
  background: #f6f6f6;
  color: #ce7d88;
  border: solid 1px #EAC7CC;
}

.more-padding {
  padding-top: 35px;
  display: flex;
  align-items: center;
  flex-direction: column;

}

.more-padding input {
  padding: 12px;
}

.more-padding .submit {
  margin-top: 30px;
}

.submit {
  margin-top: 25px;
  padding: 12px;
  border-color: #ce7d88;
}

.submit:hover {
  background: #CBC0D3;
  border-color: #bfb1c9;
}

.el-input {
  background: #EAC7CC;
  width: 100%;
  height: 40px;
  color: #ce7d88;
  border: none;
  border-bottom: 1px solid rgba(246, 246, 246, 0.5);
}

.el-input::placeholder {
  color: #f6f6f6;
  letter-spacing: 2px;
  font-size: 1.3em;
  font-weight: 100;
}

.el-input:focus {
  color: #ce7d88;
  outline: none;
  border-bottom: 1.2px solid rgba(206, 125, 136, 0.7);
  font-size: 1em;
  transition: 0.8s all ease;
}

.el-input:focus::placeholder {
  opacity: 0;
}

label {
  font-family: "Open Sans", sans-serif;
  color: #ce7d88;
  font-size: 0.8em;
  letter-spacing: 1px;
}

.checkbox {
  display: inline;
  white-space: nowrap;
  position: relative;
  left: -62px;
  top: 5px;
}

.el-input[type=checkbox] {
  width: 7px;
  background: #ce7d88;
}

.checkbox .el-input[type=checkbox]:checked + label {
  color: #ce7d88;
  transition: 0.5s all ease;
}

.login-code {
  width: 33%;
  height: 40px;
  float: right;

  img {
    width: 100%;
    cursor: pointer;
    vertical-align: middle;
  }
}
</style>
